Source Code Viewer - Example 10 - PrimeMinister2Form

package name.matthewgreet.example10.form;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;

import org.apache.commons.beanutils.PropertyUtils;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.interceptor.ValidationAware;

import name.matthewgreet.example10.dto.DateRange;
import name.matthewgreet.example10.dto.PrimeMinisterDTO;
import name.matthewgreet.example10.dto.PrimeMinisterDTO.Party;
import name.matthewgreet.strutscommons.annotation.IntegerConversion;
import name.matthewgreet.strutscommons.annotation.Required;
import name.matthewgreet.strutscommons.annotation.Trim;
import name.matthewgreet.strutscommons.form.AbstractFormattableForm;
import name.matthewgreet.strutscommons.form.FormatResult;

@SuppressWarnings("deprecation")
public class PrimeMinister2Form extends AbstractFormattableForm {
	public static class FormattedDateComponents implements Serializable {
		private static final long serialVersionUID = 140002736971690567L;
		
		private String day;
		private String month;
		private String year;
		
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		
		public String getMonth() {
			return month;
		}
		public void setMonth(String month) {
			this.month = month;
		}
		
		public String getYear() {
			return year;
		}
		public void setYear(String year) {
			this.year = year;
		}
		
		
		@Override
		public String toString() {
			return "FormattedDateComponents [day=" + day + ", month=" + month + ", year=" + year + "]";
		}
		
	}
	
	private static final long serialVersionUID = -6937072630784482008L;
	
	@Trim
	@Required(message = "A name is required")
	private String name;
	
	private String dayOfBirth;
	private String monthOfBirth;
	private String yearOfBirth;
	private Date dateOfBirth;
	
	private List<String> startDayOfOffice;
	private List<String> startMonthOfOffice;
	private List<String> startYearOfOffice;
	private List<String> endDayOfOffice;
	private List<String> endMonthOfOffice;
	private List<String> endYearOfOffice;
	private List<DateRange> termsOfOffice;
	
	private Party party;
	
	@IntegerConversion(message = "Max Seats Won must be blank or a number")
	private Integer maxSeatsWon;
	
	

	public PrimeMinister2Form() {
		resetFields();
	}
	
	public PrimeMinister2Form(PrimeMinisterDTO primeMinister) throws Exception {
		PropertyUtils.copyProperties(this, primeMinister);
	}
	
	public static FormattedDateComponents formatDate(Date date) {
		Calendar calendar;
		FormattedDateComponents result;
		
		result = new FormattedDateComponents();
		if (date != null) {
			calendar = new GregorianCalendar();
			calendar.setTime(date);
			result.setDay(Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
			result.setMonth(Integer.toString(calendar.get(Calendar.MONTH) + 1));
			result.setYear(Integer.toString(calendar.get(Calendar.YEAR)));
		} else {
			result.setDay("");
			result.setMonth("");
			result.setYear("");
		}
		return result;
	}
	
	@Override
	public FormatResult format() {
		FormattedDateComponents formattedDateComponents, startDateComponents, endDateComponents;
		FormatResult result;
		
		result = new FormatResult();
		
		formattedDateComponents = formatDate(dateOfBirth);
		dayOfBirth = formattedDateComponents.getDay();
		monthOfBirth = formattedDateComponents.getMonth();
		yearOfBirth = formattedDateComponents.getYear();
		
		startDayOfOffice = new ArrayList<>();
		startMonthOfOffice = new ArrayList<>();
		startYearOfOffice = new ArrayList<>();
		endDayOfOffice = new ArrayList<>();
		endMonthOfOffice = new ArrayList<>();
		endYearOfOffice = new ArrayList<>();
		for (DateRange termOfOffice: termsOfOffice) {
			startDateComponents = formatDate(termOfOffice.getStart());
			startDayOfOffice.add(startDateComponents.getDay());
			startMonthOfOffice.add(startDateComponents.getMonth());
			startYearOfOffice.add(startDateComponents.getYear());
			endDateComponents = formatDate(termOfOffice.getEnd());
			endDayOfOffice.add(endDateComponents.getDay());
			endMonthOfOffice.add(endDateComponents.getMonth());
			endYearOfOffice.add(endDateComponents.getYear());
		}
		
		return result;
	}
	
	@Override
	public void resetFields() {
		name = "";
		dayOfBirth = "";
		monthOfBirth = "";
		yearOfBirth = "";
		dateOfBirth = null;
		startDayOfOffice = new ArrayList<>();
		startMonthOfOffice = new ArrayList<>();
		startYearOfOffice = new ArrayList<>();
		endDayOfOffice = new ArrayList<>();
		endMonthOfOffice = new ArrayList<>();
		endYearOfOffice = new ArrayList<>();
		termsOfOffice = new ArrayList<>();
		party = null;
		maxSeatsWon = null;
	}
	
	public static Date validateDate(String day, String month, String year) {
		Calendar calendar;
		int parsedDay, parsedMonth, parsedYear;
		
		if (day != null && day.length() > 0) {
			parsedDay = Integer.parseInt(day);
			parsedMonth = Integer.parseInt(month);
			parsedYear = Integer.parseInt(year);
			
			calendar = new GregorianCalendar();
			calendar.set(parsedYear, parsedMonth-1, parsedDay);
			calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			calendar.setLenient(false);
			return calendar.getTime();
		} else {
			return null;
		}
	}

	@Override
	public void doValidate(ValidationAware validationAware, TextProvider textProvider) {
		Date termOfOfficeStart, termOfOfficeEnd;
		String endDay, month, year;
		int i;
		
		try {
			dateOfBirth = validateDate(dayOfBirth, monthOfBirth, yearOfBirth);
		}
		catch (NumberFormatException e) {
			validationAware.addActionError("Day, month, or year is not a number, you should not see this message");
		}
		catch (IllegalArgumentException e) {
			validationAware.addActionError("This day, month and year combination for date of birth doesn't exist in the calendar");
		}

		
		termsOfOffice = new ArrayList<>();
		try {
			i = 0;
			for (String startDay: startDayOfOffice) {
				month = startMonthOfOffice.get(i);
				year = startYearOfOffice.get(i);
				termOfOfficeStart = validateDate(startDay, month, year);
				endDay = endDayOfOffice.get(i);
				month = endMonthOfOffice.get(i);
				year = endYearOfOffice.get(i);
				termOfOfficeEnd = validateDate(endDay, month, year);
				termsOfOffice.add(new DateRange(termOfOfficeStart, termOfOfficeEnd));
				i++;
			}
		}
		catch (NumberFormatException e) {
			validationAware.addActionError("Day, month, or year is not a number, you should not see this message");
		}
		catch (IllegalArgumentException e) {
			validationAware.addActionError("This day, month and year combination for term of office doesn't exist in the calendar");
		}
	}

	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDayOfBirth() {
		return dayOfBirth;
	}
	public void setDayOfBirth(String dayOfBirth) {
		this.dayOfBirth = dayOfBirth;
	}

	public String getMonthOfBirth() {
		return monthOfBirth;
	}
	public void setMonthOfBirth(String monthOfBirth) {
		this.monthOfBirth = monthOfBirth;
	}

	public String getYearOfBirth() {
		return yearOfBirth;
	}
	public void setYearOfBirth(String yearOfBirth) {
		this.yearOfBirth = yearOfBirth;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}
	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}
	
	public List<String> getStartDayOfOffice() {
		return startDayOfOffice;
	}

	public void setStartDayOfOffice(List<String> startDayOfOffice) {
		this.startDayOfOffice = startDayOfOffice;
	}

	public List<String> getStartMonthOfOffice() {
		return startMonthOfOffice;
	}

	public void setStartMonthOfOffice(List<String> startMonthOfOffice) {
		this.startMonthOfOffice = startMonthOfOffice;
	}

	public List<String> getStartYearOfOffice() {
		return startYearOfOffice;
	}

	public void setStartYearOfOffice(List<String> startYearOfOffice) {
		this.startYearOfOffice = startYearOfOffice;
	}

	public List<String> getEndDayOfOffice() {
		return endDayOfOffice;
	}

	public void setEndDayOfOffice(List<String> endDayOfOffice) {
		this.endDayOfOffice = endDayOfOffice;
	}

	public List<String> getEndMonthOfOffice() {
		return endMonthOfOffice;
	}

	public void setEndMonthOfOffice(List<String> endMonthOfOffice) {
		this.endMonthOfOffice = endMonthOfOffice;
	}

	public List<String> getEndYearOfOffice() {
		return endYearOfOffice;
	}

	public void setEndYearOfOffice(List<String> endYearOfOffice) {
		this.endYearOfOffice = endYearOfOffice;
	}

	public List<DateRange> getTermsOfOffice() {
		return termsOfOffice;
	}
	public void setTermsOfOffice(List<DateRange> termsOfOffice) {
		this.termsOfOffice = termsOfOffice;
	}
	
	public Party getParty() {
		return party;
	}
	public void setParty(Party party) {
		this.party = party;
	}
	
	public Integer getMaxSeatsWon() {
		return maxSeatsWon;
	}
	public void setMaxSeatsWon(Integer maxSeatsWon) {
		this.maxSeatsWon = maxSeatsWon;
	}
	
	
	
	@Override
	public String toString() {
		final int maxLen = 30;
		return "PrimeMinister2Form [name=" + name + ", dayOfBirth=" + dayOfBirth + ", monthOfBirth=" + monthOfBirth
				+ ", yearOfBirth=" + yearOfBirth + ", dateOfBirth=" + dateOfBirth + ", startDayOfOffice="
				+ (startDayOfOffice != null ? startDayOfOffice.subList(0, Math.min(startDayOfOffice.size(), maxLen))
						: null)
				+ ", startMonthOfOffice="
				+ (startMonthOfOffice != null
						? startMonthOfOffice.subList(0, Math.min(startMonthOfOffice.size(), maxLen))
						: null)
				+ ", startYearOfOffice="
				+ (startYearOfOffice != null ? startYearOfOffice.subList(0, Math.min(startYearOfOffice.size(), maxLen))
						: null)
				+ ", endDayOfOffice="
				+ (endDayOfOffice != null ? endDayOfOffice.subList(0, Math.min(endDayOfOffice.size(), maxLen)) : null)
				+ ", endMonthOfOffice="
				+ (endMonthOfOffice != null ? endMonthOfOffice.subList(0, Math.min(endMonthOfOffice.size(), maxLen))
						: null)
				+ ", endYearOfOffice="
				+ (endYearOfOffice != null ? endYearOfOffice.subList(0, Math.min(endYearOfOffice.size(), maxLen))
						: null)
				+ ", termsOfOffice="
				+ (termsOfOffice != null ? termsOfOffice.subList(0, Math.min(termsOfOffice.size(), maxLen)) : null)
				+ ", party=" + party + ", maxSeatsWon=" + maxSeatsWon + "]";
	}

}